package com.zzh.partnersys.im.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.Gson;
import com.zzh.partnersys.im.message.request.TeamChatEmojiRequestMessage;
import com.zzh.partnersys.im.rocketmq.contant.MQConstant;
import com.zzh.partnersys.im.rocketmq.producer.MessageSender;
import com.zzh.partnersys.im.session.WSTeamSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

/**
 * @author ??????
 * @version 1.0
 */
//ר��ֻ����TeamChatEmojiRequestMessage���͵���Ϣ��Handler,����Ⱥ��
@Sharable
public class TeamChatEmojiRequestMessageHandler extends SimpleChannelInboundHandler<TeamChatEmojiRequestMessage> {
    private Gson gson = new Gson();

    private MessageSender messageSender;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TeamChatEmojiRequestMessage teamChatEmojiRequestMessage) throws Exception {
        System.out.println("teamChatEmojiRequestMessage"+ teamChatEmojiRequestMessage);
        //��ȡ���ݣ�������Ϣ��??
        if (teamChatEmojiRequestMessage == null){

        }
        Long teamId = teamChatEmojiRequestMessage.getTeamId();//�����ĸ�??

        //�ж����ߵ���??
        List<Channel> membersChannel = WSTeamSessionFactory.getSession().getMembersChannel(teamId);
        if (!membersChannel.isEmpty()){
            //������??
            for (Channel channel : membersChannel) {
                if (channel != channelHandlerContext.channel()){
                    System.out.println("����");
                    String json = gson.toJson(teamChatEmojiRequestMessage);
                    channel.writeAndFlush(new TextWebSocketFrame(json));
                }

            }
        }
        //rocketMQ������??
        if (messageSender == null){
            messageSender = getMessageSender();
        }
        messageSender.sendMsg(MQConstant.CHAT_GROUP_EMOTION_TOPIC,teamChatEmojiRequestMessage);

    }
    protected MessageSender getMessageSender(){
        return SpringUtil.getBean(MessageSender.class);
    }

}
